package NWPackage;

import java.util.HashMap;


public class RankDist {
	private HashMap<Integer, Double> rank;
	private double sumProbs;
	
	public RankDist()
	{
		this.rank = new HashMap<Integer, Double>();
		this.sumProbs = 0.0;
	}
	public void addRank(int vote, double probability)
	{
		this.rank.put(vote, probability);
		this.sumProbs += probability;
	}
	public double getVoteProb(int vote) {
		if (!rank.isEmpty() && rank.containsKey(vote))
			return this.rank.get(vote);
		else
			return 0;
	}
	
	public double getSumProbs()
	{
		return sumProbs;
	}
	
	public void changeRank(int vote, double probability) throws Exception
	{
		if (!(this.rank.containsKey(vote)))
			throw new Exception("no such vote to change");
		else
		{
			this.sumProbs -= this.rank.get(vote);
			this.sumProbs += probability;
			this.rank.put(vote, probability);
		}

	}
	public int getNumOfVotes() {
		return this.rank.size();
	}
	public double calcSumWeightedWithOSDM(OneSettingDistMatrix osdm, String agentName, String candidate) {
		double ans = 0.0;
		for (Integer vote : rank.keySet()) {
			ans += osdm.getVoteProb(agentName, candidate, vote)* this.rank.get(vote);
		}
		return ans;
	}
	public void fixRanks(int i) {
		for (Integer rankNumber : rank.keySet()) {
			if (rankNumber == i)
				rank.put(rankNumber, 1.0);
			else
				rank.put(rankNumber, 0.0);
		}
		
	}
	
	public double getEntropyOfVector()
	{
		return (-1.0) * sumProbs * Calculations.Log2(sumProbs);
	}
	public void replaceRankDistWith(RankDist rankDist) {
		for (Integer vote : this.rank.keySet()) {
			this.rank.put(vote, rankDist.getVoteProb(vote));
		}
		this.sumProbs = rankDist.sumProbs;
	}
	public RankDist clone()
	{
		RankDist rank = new RankDist();
		rank.sumProbs = this.sumProbs;
		for (Integer vote : this.rank.keySet()) {
			rank.addRank(vote, this.rank.get(vote));
		}
		return rank;
	}
}
